package com.woniu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.dto.BloodDto;
import com.woniu.dto.ImageDto;
import com.woniu.dto.QueryBloodDto;
import com.woniu.dto.QueryImageDto;
import com.woniu.exception.CheckException;
import com.woniu.model.HisBlood;
import com.woniu.model.HisImage;
import com.woniu.model.HisImage;
import com.woniu.mapper.HisImageMapper;
import com.woniu.param.CreateImageParam;
import com.woniu.param.QueryImageParam;
import com.woniu.service.IHisImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 检查影像表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisImageServiceImpl extends ServiceImpl<HisImageMapper, HisImage> implements IHisImageService {


    @Override
    public QueryImageDto queryImage(QueryImageParam queryImageParam) {
        //时间倒序查找
        QueryWrapper<HisImage> wrapper = new QueryWrapper<HisImage>().orderByDesc("raw_add_time");

        //查询条件
        if (ObjectUtil.isNotNull(queryImageParam.getImageId())) {
            wrapper.eq("image_id", queryImageParam.getImageId());
        }
        if (ObjectUtil.isNotNull(queryImageParam.getImageProject())) {
            wrapper.eq("image_project", queryImageParam.getImageProject());
        }
        if (ObjectUtil.isNotNull(queryImageParam.getCaseId())) {
            wrapper.eq("case_id", queryImageParam.getCaseId());
        }
        if (ObjectUtil.isNotNull(queryImageParam.getCheckId())) {
            wrapper.eq("check_id", queryImageParam.getCheckId());
        }

        List<HisImage> imageList = this.list(wrapper);

        if(ObjectUtil.isNull(imageList)) {
            throw new CheckException(1011, "无影像结果");
        }

        QueryImageDto dto = new QueryImageDto();
        List<ImageDto> imageDtoList = CopyBeanUtil.copyBean(imageList, ImageDto.class);
        dto.setImageDtoList(imageDtoList);

        return dto;
    }

    @Override
    public QueryImageDto createImage(CreateImageParam createImageParam) {
        HisImage image = new HisImage();
        BeanUtils.copyProperties(createImageParam, image);

        if (!this.save(image)) {
            throw new CheckException(1003, "影像结果保存失败");
        }

        QueryImageDto dto = new QueryImageDto();
        BeanUtils.copyProperties(image, dto);
        return dto;
    }
}
